//
//  DialRecoredViewController.m
//  ChengXinBao
//
//  Created by RongZhou on 17/2/15.
//  Copyright © 2017年 RongZhou. All rights reserved.
//

#import "DialRecoredViewController.h"
#import "DialDetailInforCell.h"
@interface DialRecoredViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dayArray;
@property(nonatomic,strong)NSMutableArray *timeArray;
@property(nonatomic,strong)NSMutableDictionary *timeDict;
@property(nonatomic,strong)NSArray *sortArray;
@end

@implementation DialRecoredViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self crateUIAboutTableView];
    [self getAllDetailDialInformation];
}
-(void)getAllDetailDialInformation{
    dispatch_queue_t gloabQueue = dispatch_get_global_queue(0, 0);
    dispatch_async(gloabQueue, ^{
        NSMutableArray *array1 = [[NSMutableArray alloc] init];
        for (CallModel *model in self.userInforArray) {
            [array1 addObject:model.userCallTime];
        }
        for (NSInteger i = 0; i < array1.count; i++) {
            NSString *str = [NSString stringWithFormat:@"%@",array1[i]];
            NSArray *timeArra = [str componentsSeparatedByString:@" "];
            [self.dayArray addObject:timeArra[0]];
            [self.timeArray addObject:timeArra[1]];
        }
        self.timeDict = [[NSMutableDictionary alloc] init];
        for (NSInteger i = 0; i < self.dayArray.count; i++) {
            if ([self.dayArray[i] isEqual:@""]) {
                continue;
            }
            NSMutableArray *timeAll = [[NSMutableArray alloc] init];
            [timeAll addObject:self.timeArray[i]];
            for (NSInteger j = i + 1; j < self.dayArray.count; j++) {
                if ([self.dayArray[i] isEqual: self.dayArray[j]]) {
                    [self.dayArray replaceObjectAtIndex:j withObject:@""];
                    [timeAll addObject:self.timeArray[j]];
                }
            }
            [self.timeDict setObject:timeAll forKey:self.dayArray[i]];
        }
        self.sortArray = [self.timeDict allKeys];
        self.sortArray = (NSMutableArray *)[self.sortArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"YYYY年-MM月-dd日"];
            if (obj1 == [NSNull null]) {
                obj1 = @"0000年-00月-00日";
            }
            if (obj2 == [NSNull null]) {
                obj2 = @"0000年-00月-00日";
            }
            NSDate *date1 = [formatter dateFromString:obj1];
            NSDate *date2 = [formatter dateFromString:obj2];
            NSComparisonResult result = [date1 compare:date2];
            return result == NSOrderedAscending;
        }];
        NSLog(@"数组%@",self.sortArray);
        NSLog(@"我是字典%@",self.timeDict);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });
    
   
    
}
-(void)crateUIAboutTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 300) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectZero];
    footerView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = footerView;
    [self.view addSubview:self.tableView];
    UINib *nib = [UINib nibWithNibName:@"DialDetailInforCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"DialDetailInforCell"];
}
#pragma mark - tableview的代理方法的实现
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.timeDict allKeys].count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [self.timeDict allKeys];
    NSString *key = array[section];
    NSArray *arra = self.timeDict[key];
    NSLog(@"数组的长度%lu",(unsigned long)arra.count);
    return arra.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    NSString *key = [NSString stringWithFormat:@"%@",self.sortArray[section]];
    NSArray *arra = [self.timeDict valueForKey:key];
    NSMutableArray *arry = [NSMutableArray arrayWithArray:arra];
    NSLog(@"数组的长度%lu",(unsigned long)arra.count);
    for (NSInteger i = 0; i < arry.count/2; i++) {
        [arry exchangeObjectAtIndex:i withObjectAtIndex:arry.count - 1 - i];
    }
    static NSString *cellId = @"DialDetailInforCell";
    DialDetailInforCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    cell.timeLabel.text = [NSString stringWithFormat:@"%@",arry[row]];
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.sortArray[section];
}
-(NSMutableArray *)dayArray{
    if (_dayArray == nil) {
        _dayArray = [[NSMutableArray alloc] init];
    }
    return _dayArray;
}
-(NSMutableArray *)timeArray{
    if (_timeArray == nil) {
        _timeArray = [[NSMutableArray alloc] init];
    }
    return _timeArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

